var express = require('express');
var router = express.Router();
const { HtypeModel, Hmodel, cartmodel } = require('../module/sunt')




router.post('/add-type', async (req, res) => {
    await HtypeModel.create(req.body)
    res.send('ok')
})

router.post('/add', async (req, res) => {

    await Hmodel.create(req.body)
    res.send('ok')
})


router.get('/get-type', async (req, res) => {
    let data = await HtypeModel.find().populate('text')
    res.send(data)
})


router.get('/get-list', async (req, res) => {
    let data = await Hmodel.find()
    res.send(data)
})

router.put('/update-num', async (req, res) => {
    await Hmodel.updateOne({ _id: req.body.id }, { num: req.body.num })
    res.send('ok')
})


router.get('/list-card', async (req, res) => {
    let data = await cartmodel.find()
    res.send(data)
})



router.post('/update-num2', async (req, res) => {
    await cartmodel.updateOne({ _id: req.body._id }, req.body)
    res.send('ok')
})




router.post('/add-card', async (req, res) => {
    await cartmodel.create(req.body)
    await Hmodel.updateMany({ num: 0 })
    res.send({
        code: 200,
        msg: '添加成功'
    })
})


router.put('/update-card', async (req, res) => {
    await cartmodel.updateOne({ userid: req.body.userid }, req.body)
    res.send({
        code: 200,
        msg: '修改成功'
    })
})


module.exports = router;
